﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
using Utilities.QuickSql;
using System.Globalization;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Net;


namespace showcolat
{
    public partial class fen_liste : Form
    {
        MySqlDatabase db = new MySqlDatabase("localhost", "root", "", "showcolat");
        public fen_liste()
        {
            InitializeComponent();
            int i = 1;
            Label lab_serie;
            Button B_favoris;
            Table serie = db.QueryTable("SELECT id_serie, nom_serie FROM serie ");
            int y = 100;
            foreach (Record s in serie)
            {
                this.Controls.Add(lab_serie = new Label() { Name = s[0].ToString(), AutoSize = true, Location = new Point(50, y), Text = s[1].ToString(), Font = new Font("Arial",10)});
                this.Controls.Add(B_favoris = new Button() { Name = "bouton", Location = new Point(lab_serie.Location.X+lab_serie.Size.Width, y), Text = "favoris", Font = new Font("Arial", 10) });
                y += 50;
                lab_serie.Click += new EventHandler(lbl_Click);
                i++;
            }
        }

        public fen_liste(int id_serie)
        {
            InitializeComponent();
            lab_allSerie.Text = "Liste des épisodes";
            List<Episode> listeEpisode;
            string recherche_ep = "";
            recherche_ep = id_serie.ToString();
            listeEpisode = FindEpisode(recherche_ep);
            Label episode;
            int y = 50;

                foreach (Episode ep in listeEpisode)
                {
                    if (ep.IDSaison != 0)
                    {
                        this.Controls.Add(episode = new Label
                        {
                            Name = ep.IDEpisode.ToString(),
                            AutoSize = true,
                            Location = new Point(50, y),
                            Text = ep.TitreEpisode + " / "+ep.Diffusion,
                            Size = new System.Drawing.Size(200, 50),
                            Font = new Font("Arial", 10)
                        });
                        y += 50;
                    }
                }
            

        }

        public fen_liste(string id_serie)
        {
            InitializeComponent();
            int i = 1;
            lab_allSerie.Text = "Liste des épisodes";
            Label lab_serie;
            Table serie = db.QueryTable("SELECT E.titre_episode FROM Episode E INNER JOIN saison S ON s.id_saison = E.id_saison INNER JOIN serie SE ON SE.id_serie = S.id_serie WHERE SE.id_serie ='"+id_serie.ToString()+"'");
            int y = 75;
            foreach (Record s in serie)
            {
                this.Controls.Add(lab_serie = new Label() { Name = s[0].ToString(), AutoSize = true, Location = new Point(50, y), Text = s[0].ToString(), Font = new Font("Arial", 10) });
                y += 50;
                i++;
            }
        }

        void lbl_Click(object sender, EventArgs e)
        {

            Label l = (Label)sender;
            Form_description Desc = new Form_description(l.Name);
            Desc.ShowDialog();
        }

        public static string GetPageAsString(Uri address)
        {
            string result = "";

            // Create the web request  
            HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

            // Get response  
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                // Get the response stream  
                StreamReader reader = new StreamReader(response.GetResponseStream());

                // Read the whole contents and return as a string  
                result = reader.ReadToEnd();
            }

            return result;
        }

        public static DataSet FetchXml(string uri)
        {
            // Create the web request  
            HttpWebRequest request
                = WebRequest.Create(uri) as HttpWebRequest;

            // Get response  
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                // Load data into a dataset  
                DataSet dsSeries = new DataSet();
                dsSeries.ReadXml(response.GetResponseStream());
                // Print dataset information  
                return dsSeries;
                //return PrintDataSet(dsSeries);
            }
        }


        public static List<Episode> FindEpisode(string NumEpisode)
        {
            List<Episode> liste = new List<Episode>();
            int i = 0;
            DataSet episodes = FetchXml("http://www.thetvdb.com//data/series/" + NumEpisode + "/all/fr.xml");
            foreach (DataTable table in episodes.Tables)
            {
                if (table.TableName == "Episode")
                {
                    foreach (DataRow row in table.Rows)
                    {
                        int EpisodeId = int.Parse(row["id"].ToString());
                        int SeasonNumber = int.Parse(row["Combined_season"].ToString());
                        string EpisodeNumber = row["Combined_episodenumber"].ToString();
                        int EpisodeLength = 0;
                        string EpisodeName = row["EpisodeName"].ToString();
                        string EpisodeRating = row["Rating"].ToString();
                        string Diffusion = row["FirstAired"].ToString();
                        liste.Add(new Episode(EpisodeId, EpisodeName, EpisodeNumber, SeasonNumber, EpisodeLength, int.Parse(NumEpisode), Diffusion));
                        if (i == 5)
                        {
                            Console.ReadKey(true);
                            i = 0;
                        }
                    }
                }
            }
            return liste;
        }


        public static void PrintDataSet(DataSet ds)
        {
            int i = 0;
            List<Serie> liste = new List<Serie>(5);
            // Print out all tables and their columns  
            foreach (DataTable table in ds.Tables)
            {

                Console.WriteLine("TABLE '{0}'", table.TableName);
                Console.WriteLine("Total # of rows: {0}", table.Rows.Count);
                Console.WriteLine("---------------------------------------------------------------");

                foreach (DataColumn column in table.Columns)
                {
                    Console.WriteLine("- {0} ({1})", column.ColumnName, column.DataType.ToString());
                }


                Console.WriteLine(System.Environment.NewLine);

            }
        }

        static string convertUri(string serie)
        {
            return "http://www.thetvdb.com/api/GetSeries.php?seriesname=" + serie + "&language=fr";
        }

     }
    
}
